<?php
require_once("lib/db_connection.php");
$company_id = $_SESSION['company_id'];
$p1 = $_GET['p1'];
$p2 = $_GET['p2'];
$p3 = $_GET['p3'];

if($p2=='LIKE')
$passed_where = $p1." ".$p2." '%".$p3."%'";
if($p2!='LIKE')
$passed_where = $p1." ".$p2." '".$p3."'";

if($p1==''||$p1==null)
{
$query_veh = "SELECT 
vehicle_tbl.*,vehicle_tbl.chassis_num as c_num,vehicle_tbl.year as v_year,vehicle_tbl.customer as v_cust,
veh_purchase_tbl.*,veh_purchase_tbl.date as p_date,veh_purchase_tbl.price as p_price,veh_purchase_tbl.expiration_date as p_exp,
veh_engine_tbl.*,
veh_insurance_tbl.*,
veh_registration_tbl.*,veh_registration_tbl.date as r_date,
veh_depreciation_tbl.*,
veh_fluids_tbl.*,
gps_tbl.*,
veh_disposal_tbl.*,
veh_chassis_tbl.* 
FROM vehicle_tbl 
LEFT JOIN veh_purchase_tbl 
ON vehicle_tbl.vehicle_id=veh_purchase_tbl.vehicle_id
LEFT JOIN veh_engine_tbl 
ON vehicle_tbl.vehicle_id=veh_engine_tbl.vehicle_id
LEFT JOIN veh_insurance_tbl 
ON vehicle_tbl.vehicle_id=veh_insurance_tbl.vehicle_id
LEFT JOIN veh_registration_tbl 
ON vehicle_tbl.vehicle_id=veh_registration_tbl.vehicle_id
LEFT JOIN veh_depreciation_tbl 
ON vehicle_tbl.vehicle_id=veh_depreciation_tbl.vehicle_id
LEFT JOIN veh_fluids_tbl 
ON vehicle_tbl.vehicle_id=veh_fluids_tbl.vehicle_id
LEFT JOIN gps_tbl 
ON vehicle_tbl.vehicle_id=gps_tbl.vehicle_id
LEFT JOIN veh_disposal_tbl 
ON vehicle_tbl.vehicle_id=veh_disposal_tbl.vehicle_id
LEFT JOIN veh_chassis_tbl 
ON vehicle_tbl.vehicle_id=veh_chassis_tbl.vehicle_id
LEFT JOIN veh_other_details_tbl 
ON vehicle_tbl.vehicle_id=veh_other_details_tbl.vehicle_id
WHERE vehicle_tbl.company_id='".$company_id."'";
}

if($p1!=''||$p1!=null)
{
$query_veh = "SELECT 
vehicle_tbl.*,vehicle_tbl.chassis_num as c_num,vehicle_tbl.year as v_year,vehicle_tbl.customer as v_cust,
veh_purchase_tbl.*,veh_purchase_tbl.date as p_date,veh_purchase_tbl.price as p_price,veh_purchase_tbl.expiration_date as p_exp,
veh_engine_tbl.*,
veh_insurance_tbl.*,
veh_registration_tbl.*,veh_registration_tbl.date as r_date,
veh_depreciation_tbl.*,
veh_fluids_tbl.*,
gps_tbl.*,
veh_disposal_tbl.*,
veh_chassis_tbl.* 
FROM vehicle_tbl 
LEFT JOIN veh_purchase_tbl 
ON vehicle_tbl.vehicle_id=veh_purchase_tbl.vehicle_id
LEFT JOIN veh_engine_tbl 
ON vehicle_tbl.vehicle_id=veh_engine_tbl.vehicle_id
LEFT JOIN veh_insurance_tbl 
ON vehicle_tbl.vehicle_id=veh_insurance_tbl.vehicle_id
LEFT JOIN veh_registration_tbl 
ON vehicle_tbl.vehicle_id=veh_registration_tbl.vehicle_id
LEFT JOIN veh_depreciation_tbl 
ON vehicle_tbl.vehicle_id=veh_depreciation_tbl.vehicle_id
LEFT JOIN veh_fluids_tbl 
ON vehicle_tbl.vehicle_id=veh_fluids_tbl.vehicle_id
LEFT JOIN gps_tbl 
ON vehicle_tbl.vehicle_id=gps_tbl.vehicle_id
LEFT JOIN veh_disposal_tbl 
ON vehicle_tbl.vehicle_id=veh_disposal_tbl.vehicle_id
LEFT JOIN veh_chassis_tbl 
ON vehicle_tbl.vehicle_id=veh_chassis_tbl.vehicle_id
LEFT JOIN veh_other_details_tbl 
ON vehicle_tbl.vehicle_id=veh_other_details_tbl.vehicle_id
WHERE vehicle_tbl.company_id='".$company_id."'
AND ".$passed_where;	
}
$result_veh = mysql_query($query_veh)or die(mysql_error());

//SEND TO EXCEL
function SendToExcel(&$str) 
{ 
$str = preg_replace("/\t/", "\\t", $str); 
$str = preg_replace("/\r?\n/", "\\n", $str); 
if(strstr($str, '"')) 
$str = '"' . str_replace('"', '""', $str) . '"'; 
}
$data = array();
while($row_veh = mysql_fetch_array($result_veh))
{
			//TRACKING
			$tracking = $row_veh['track_by'];
			if($tracking=='distance')
			$curr_od = $row_veh['curr_tracking_value'].'Km';
			if($tracking=='hours')
			$curr_od = $row_veh['curr_hours'].'Hrs';
			
			//FLEET CARD
			$f_card = $row_veh['fleet_card'];
			if($f_card==1)
			$fcard = 'Yes';
			if($f_card==0)
			$fcard = 'No';
			if($f_card==null)
			$fcard = ' ';
			
			$data[] = array(
			//VEHICLE DETAILS
			"Plate No" => $row_veh["veh_num"], 
			"Year" => $row_veh["v_year"], 
			"Make" => $row_veh["manufacturer_name"], 
			"Model" => $row_veh["model_name"],
			"Total Mileage" => $curr_od, 
			"Ownership" => $row_veh["ownership"], 
			"Owners Name" => $row_veh["customer"], 
			"Driver" => $row_veh["operator"],
			"Engine No" => $row_veh["engine_model"], 
			"Chassis No" => $row_veh["c_num"], 
			"Department" => $row_veh["department"], 
			"Garage" => $row_veh["location"],
			
			//PURCHASE
			"Purchase Date" => $row_veh["p_date"], 
			"Price" => $row_veh["p_price"], 
			"Purchase From" => $row_veh["purchase_from"], 
			"End of Warranty" => $row_veh["p_exp"],
			
			//ENGINE
			"Code" => $row_veh["code"], 
			"Power(bhp)" => $row_veh["power_bhp"], 
			"Torque(lb-ft)" => $row_veh["torque_lb"], 
			"Disp(ci)" => $row_veh["disp_ci"],
			"Aircon Type" => $row_veh["aircon_type"], 
			"Aspiration" => $row_veh["aspiration"], 
			"Cylinder" => $row_veh["cylinder"], 
			"Fuel Type" => $row_veh["fuel_type"],
			
			//CHASSIS
			"Diff Type" => $row_veh["diff_type"], 
			"Trans Type" => $row_veh["trans_type"], 
			"Trans Code" => $row_veh["trans_code"], 
			"Ratio" => $row_veh["ratio"],
			"Front Wheel Size" => $row_veh["fwheel_size"], 
			"Front Tire Size" => $row_veh["ftire_size"], 
			"Rear Wheel Size" => $row_veh["rwheel_size"], 
			"Rear Tire Size" => $row_veh["rtire_size"],
			
			//FLUIDS
			"Oil Type" => $row_veh["oil_type"], 
			"Transmission Fluid" => $row_veh["transmission_fluid"], 
			"Freon" => $row_veh["freon"], 
			"Gear Oil" => $row_veh["gear_oil"],
			"Hydraulic Fluid" => $row_veh["hydraulic_fluid"],
			
			//LTO REGISTRATION
			"Field Office" => $row_veh["field_office"], 
			"CR No" => $row_veh["cr_no"], 
			"Registration Date" => $row_veh["r_date"], 
			"OR No" => $row_veh["or_no"],
			"OR Date" => $row_veh["or_date"],
			
			//DISPOSAL
			"Disposal Date" => $row_veh["sales_date"], 
			"Sold To" => $row_veh["buyer"], 
			"Address" => $row_veh['bldg'].' '.$row_veh['street'].' '.$row_veh['city'].','.$row_veh['country'],
			"Price" => $row_veh["price"], 
			
			//DEPRECIATION
			"Depreciation Description" => $row_veh["description"], 
			"Total Period" => $row_veh["tot_period"],
			"Current Value" => $row_veh["curr_value"], 
			"Depreciation per Period" => $row_veh["dep_per_period"],
			
			//GPS 
			"GPS ID" => $row_veh["gps_id"],
			"Sim No" => $row_veh["sim_num"], 
			"Device" => $row_veh["device"],
			
			//OTHER DETAILS
			"w/ Fleet Card" => $fcard, 
			"ID No" => $row_veh["id_no"],
			"Band" => $row_veh["band"], 
			"Benefit Plan" => $row_veh["benefit_plan"],
			"Cost Center" => $row_veh["cost_center"], 
			"Group" => $row_veh["group1"],
			"Division" => $row_veh["division"], 
			"Section" => $row_veh["section"],
			"Ref PO" => $row_veh["ref_po"], 
			"Asset No" => $row_veh["asset_no"],
			"Equipt No" => $row_veh["equipt_no"]
			);	
} 
# filename for download $filename = "website_data_" . date('Ymd') . ".xls";
$filename = "Vehicle_Report".".xls";
header("Content-Disposition: attachment; filename=\"$filename\""); 
header("Content-Type: application/vnd.ms-excel"); 
$flag = false; 
foreach($data as $row) 
{ 
if(!$flag) 
{ 
# display field/column names as first row 
echo implode("\t", array_keys($row)) . "\r\n"; 
$flag = true; 
} 
array_walk($row, 'SendToExcel'); 
echo implode("\t", array_values($row)) . "\r\n"; 
} 
exit; 
?>